Imports System.Data.SqlClient
Public Class cls_linea_

    Implements ICloneable

#Region "Variables ..."
    Private int_idlinea As Integer = 0
    Private str_nombre As String = ""
    Private int_alcance As Integer = 0
    Private int_idsistema As Integer = 0
    Private int_idsubsistema As Integer = 0
    Private int_idmaterial As Integer = 0
    Private int_idregistro As Integer = 0
    Private int_idobra As Integer = 0

    Private obj_registro As cls_registro_ = Nothing
    Private obj_sistema As cls_sistema_ = Nothing
    Private obj_subsistema As cls_subsistema_ = Nothing
    Private obj_material As cls_material_ = Nothing
    Private col_Items As Collections.Generic.List(Of cls_linea_) = Nothing

    Public Overrides Function ToString() As String
        Return nombre
    End Function
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idlinea
        End Get
    End Property
    Property idlinea() As Integer
        Get
            idlinea = int_idlinea
        End Get
        Set(ByVal value As Integer)
            int_idlinea = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property alcance() As Integer
        Get
            alcance = int_alcance
        End Get
        Set(ByVal value As Integer)
            int_alcance = value
        End Set
    End Property
    Property idsistema() As Integer
        Get
            idsistema = int_idsistema
        End Get
        Set(ByVal value As Integer)
            int_idsistema = value
        End Set
    End Property
    Property idsubsistema() As Integer
        Get
            idsubsistema = int_idsubsistema
        End Get
        Set(ByVal value As Integer)
            int_idsubsistema = value
        End Set
    End Property
    Property idmaterial() As Integer
        Get
            idmaterial = int_idmaterial
        End Get
        Set(ByVal value As Integer)
            int_idmaterial = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property idobra As Integer
        Get
            idobra = int_idobra
        End Get
        Set(ByVal value As Integer)
            int_idobra = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_linea_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_linea_))
            col_Items = value
        End Set
    End Property
    Property material() As cls_material_
        Get
            If IsNothing(obj_material) Then
                obj_material = New cls_material_
                If Not obj_material.Buscar(idmaterial) Then
                    obj_material = Nothing
                End If
            End If
            material = obj_material
        End Get
        Set(ByVal value As cls_material_)
            obj_material = value
        End Set
    End Property
    Property sistema() As cls_sistema_
        Get
            If IsNothing(obj_sistema) Then
                obj_sistema = New cls_sistema_
                If Not obj_sistema.Buscar(idsistema) Then
                    obj_sistema = Nothing
                End If
            End If
            sistema = obj_sistema
        End Get
        Set(ByVal value As cls_sistema_)
            obj_sistema = value
        End Set
    End Property

    Property subsistema() As cls_subsistema_
        Get
            If IsNothing(obj_subsistema) Then
                obj_subsistema = New cls_subsistema_
                If Not obj_subsistema.Buscar(idsubsistema) Then
                    obj_subsistema = Nothing
                End If
            End If
            subsistema = obj_subsistema
        End Get
        Set(ByVal value As cls_subsistema_)
            obj_subsistema = value
        End Set
    End Property

    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_linea_)
        While obj_Reader.Read()
            Dim Elemento As New cls_linea_
            Elemento.int_idlinea = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idlinea")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("alcance"))) = False Then
                Elemento.int_alcance = (obj_Reader.GetValue(obj_Reader.GetOrdinal("alcance")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idsistema"))) = False Then
                Elemento.int_idsistema = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idsistema")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubsistema"))) = False Then
                Elemento.int_idsubsistema = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubsistema")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmaterial"))) = False Then
                Elemento.int_idmaterial = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmaterial")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idobra"))) = False Then
                Elemento.int_idobra = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idobra")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idlinea = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idlinea")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("alcance"))) = False Then
            int_alcance = (obj_Reader.GetValue(obj_Reader.GetOrdinal("alcance")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idsistema"))) = False Then
            int_idsistema = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idsistema")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubsistema"))) = False Then
            int_idsubsistema = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubsistema")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmaterial"))) = False Then
            int_idmaterial = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmaterial")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idobra"))) = False Then
            int_idobra = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idobra")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "linea__Nuevo"
                .Parameters.Add("@nombre", SqlDbType.nvarchar).Value = str_nombre
                .Parameters.Add("@alcance", SqlDbType.int).Value = int_alcance
                .Parameters.Add("@idsistema", SqlDbType.int).Value = int_idsistema
                .Parameters.Add("@idsubsistema", SqlDbType.int).Value = int_idsubsistema
                .Parameters.Add("@idmaterial", SqlDbType.int).Value = int_idmaterial
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@idobra", SqlDbType.Int).Value = int_idobra
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de linea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "linea__Modificar"
                .Parameters.Add("@idlinea", SqlDbType.int).Value = int_idlinea
                .Parameters.Add("@nombre", SqlDbType.nvarchar).Value = str_nombre
                .Parameters.Add("@alcance", SqlDbType.int).Value = int_alcance
                .Parameters.Add("@idsistema", SqlDbType.int).Value = int_idsistema
                .Parameters.Add("@idsubsistema", SqlDbType.int).Value = int_idsubsistema
                .Parameters.Add("@idmaterial", SqlDbType.int).Value = int_idmaterial
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@idobra", SqlDbType.Int).Value = int_idobra
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de linea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idlinea As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "linea__Eliminar"
                .Parameters.Add("@idlinea", SqlDbType.int).Value = idlinea
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de linea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idlinea As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "linea__Buscar"
                .Parameters.Add("@idlinea", SqlDbType.int).Value = idlinea
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de linea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "linea__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de linea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "linea__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de linea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidobra(ByVal int_idobra As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidobra = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "linea__CargarXidobra"
                .Parameters.Add("@idobra", SqlDbType.Int).Value = int_idobra
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidobra = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXidobra los registros de linea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_linea_Clon As New cls_linea_
        obj_linea_Clon.int_idlinea = Me.int_idlinea
        obj_linea_Clon.str_nombre = Me.str_nombre
        obj_linea_Clon.int_alcance = Me.int_alcance
        obj_linea_Clon.int_idsistema = Me.int_idsistema
        obj_linea_Clon.int_idsubsistema = Me.int_idsubsistema
        obj_linea_Clon.int_idmaterial = Me.int_idmaterial
        obj_linea_Clon.int_idregistro = Me.int_idregistro
        obj_linea_Clon.col_Items = Me.col_Items
        Return obj_linea_Clon
    End Function

End Class
